package org.vgou.action.manage;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.vgou.model.Location;
import org.vgou.page.Page;
import org.vgou.page.PageUntils;
import org.vgou.service.ILocationService;
import org.vgou.util.StrutsUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2011-12-1
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: LocationManageAction.java
 * @comment 地区操作类
 * 
 */
@Component("locationManageAction")
@Scope("prototype")
public class LocationManageAction  extends ActionSupport implements ModelDriven<Location>{

	private static final long serialVersionUID = -2552619456649541544L;

	protected final Log logger = LogFactory.getLog(getClass());
	
	private int page;
	private int result;
	private long locationPK;
	private long parentID;
	private long preParentID;
	private String searchName;
	private Location location = new Location();
	private List<Location> locationList;
	@Resource
	private ILocationService locationService;
	
	/**
	 * 初始化地区添加或修改表单
	 * @return
	 * @throws Exception
	 */
	public String initForm() throws Exception{
		if(locationPK > 0){
			try {
				location = locationService.getById(locationPK);
			} catch (Exception e) {
				logger.error("取得地区信息失败:" + e.getMessage());
			}
		}
		return "initForm";
	}
	
	/**
	 * 添加或修改地区信息
	 * @return
	 * @throws Exception
	 */
	public String addOrUpdate() throws Exception{
		try {
			if(location.getId() <= 0){
				long id = locationService.create(location, parentID);
				if(id > 0)
					result = 1;
				else
					result = -1;
			}else{
				result = locationService.update(location);
			}
		} catch (Exception e) {
			logger.error("更新地区信息失败:" + e.getMessage());
		}
		return "result";
	}
	
	/**
	 * 取得地区列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception{
		try {
			int perPage = 8;//每页的显示记录数
			int total = locationService.getAllCount(searchName, parentID);
			String params = null;
			if(searchName != null || parentID > 0){
				params = "name=" + searchName + "&parentID=" +parentID;
			}
			Page pageModel = PageUntils.getPageBar(StrutsUtils.getRequest(), page, perPage , total , params, "page");
			locationList = locationService.getAll(searchName, parentID, null, false, pageModel.getStartRs(), pageModel.getPerPage());
			
			if(parentID > 0){
				Location parent = locationService.getById(parentID);
				Location preParent = parent.getParent();
				if(preParent != null)
					preParentID = preParent.getId();
			}
		} catch (Exception e) {
			logger.error("取得地区列表失败:" + e.getMessage());
		}
		return "list";
	}
	
	/**
	 * 异步取得地区JSON格式
	 * @throws Excption
	 */
	public void getLocations() throws Exception{
		try {
			locationList = locationService.getAll("", parentID, "", true, 0, Integer.MAX_VALUE);
			for(Location location : locationList){
				location.setParent(null);
				location.setChildren(null);
			}
			JSONArray ja = JSONArray.fromObject(locationList);
			StrutsUtils.renderJson(ja.toString());
		} catch (Exception e) {
			logger.error("异步取得地区JSON格式失败：" + e.getMessage());
		}
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}
	
	public long getLocationPK() {
		return locationPK;
	}

	public void setLocationPK(long locationPK) {
		this.locationPK = locationPK;
	}

	public long getParentID() {
		return parentID;
	}

	public void setParentID(long parentID) {
		this.parentID = parentID;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public List<Location> getLocationList() {
		return locationList;
	}

	public void setLocationList(List<Location> locationList) {
		this.locationList = locationList;
	}

	public long getPreParentID() {
		return preParentID;
	}

	public void setPreParentID(long preParentID) {
		this.preParentID = preParentID;
	}

	@Override
	public Location getModel() {
		return location;
	}

}
